<?php

namespace app\controllers\admin\uni\user;

use app\controllers\admin\AdminController;
use app\models\UserBalance as Model;
use app\models\UserRegArea;
use app\models\UserStoreLog;
use app\services\Chain;

class BalanceController extends AdminController
{
    // 余额明细
    public function actionList()
    {
        $id = $this->post('id');
        $user_id = $this->post('user_id');
        $export = $this->post('export');

        $page = intval($this->post('page', $this->page));
        $page_size = intval($this->post('pageSize', $this->pageSize));

        $sort = $this->post('sort');

        $query = Model::find();

        if ($sort) {
            $prop = substr($sort, 0, 1);
            $prop = $prop == '-' ? SORT_DESC : SORT_ASC;
            $order = substr($sort, 1);
            $query->orderBy([$order => $prop]);
        } else {
            $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC]);
        }

        if ($id) {
            $query->andWhere(['id' => $id]);
        }
        // if ($user_id) {
        //     $query->andWhere(['user_id' => $user_id]);
        // }

        // chain
        $chain = Chain::getTeamIds($this->user->id);
        if ($user_id) {
            if (in_array($user_id, $chain)) {
                $query->andWhere(['user_id' => $user_id]);
            } else {
                $query->andWhere(['user_id' => 0]);
            }
        } else {
            $query->andWhere(['user_id' => $chain]);
        }

        // export
        if ($export) {
            return $this->success($query->asArray()->all());
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $page_size);

        $list = $query->offset(($page - 1) * $page_size)
            ->limit($page_size)
            ->asArray()
            ->all();

        return $this->success([
            $this->totalCount   => $totalCount,
            $this->totalPage    => $totalPage,
            $this->list         => $list,
        ]);

    }

    // 云仓明细
    public function actionStore()
    {
        $id = $this->post('id');
        $user_id = $this->post('user_id');
        $export = $this->post('export');

        $page = intval($this->post('page', $this->page));
        $page_size = intval($this->post('pageSize', $this->pageSize));

        $query = UserStoreLog::find();

        $query->orderBy(['id' => SORT_DESC]);

        if ($id) {
            $query->andWhere(['id' => $id]);
        }
        // if ($user_id) {
        //     $query->andWhere(['user_id' => $user_id]);
        // }

        // chain
        $chain = Chain::getTeamIds($this->user->id);
        if ($user_id) {
            if (in_array($user_id, $chain)) {
                $query->andWhere(['user_id' => $user_id]);
            } else {
                $query->andWhere(['user_id' => 0]);
            }
        } else {
            $query->andWhere(['user_id' => $chain]);
        }

        // export
        if ($export) {
            return $this->success($query->asArray()->all());
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $page_size);

        $list = $query->offset(($page - 1) * $page_size)
            ->limit($page_size)
            ->asArray()
            ->all();

        return $this->success([
            $this->totalCount   => $totalCount,
            $this->totalPage    => $totalPage,
            $this->list         => $list,
        ]);

    }

    // 区代申请列表
    public function actionAreaReg()
    {
        $page = intval($this->post('page', $this->page));
        $page_size = intval($this->post('pageSize', $this->pageSize));

        $query = UserRegArea::find();

        $query->orderBy(['id' => SORT_DESC]);


        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $page_size);

        $list = $query->offset(($page - 1) * $page_size)
            ->limit($page_size)
            ->with(['user'])
            ->asArray()
            ->all();

        foreach ($list as &$item) {
            if ($item['user']['nickname']) {
                $item['user']['nickname'] = base64_decode($item['user']['nickname']);
            }
        }

        return $this->success([
            $this->totalCount   => $totalCount,
            $this->totalPage    => $totalPage,
            $this->list         => $list,
        ]);

    }
}